//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Security;
using DotNetNuke.Services.Log.EventLog;
namespace DotNetNuke.Modules.Admin.LogViewer
{
	partial class EditLogTypes : Entities.Modules.PortalModuleBase, Entities.Modules.IActionable
	{
		protected UI.UserControls.LabelControl plThresholdNotificationTime;
		protected UI.UserControls.LabelControl plThresholdNotificationTimeType;
		private void BindDetailData()
		{
			PortalController pc = new PortalController();
			ddlLogTypePortalID.DataTextField = "PortalName";
			ddlLogTypePortalID.DataValueField = "PortalID";
			ddlLogTypePortalID.DataSource = pc.GetPortals();
			ddlLogTypePortalID.DataBind();
			ListItem i = new ListItem();
			i.Text = Services.Localization.Localization.GetString("All");
			i.Value = "*";
			ddlLogTypePortalID.Items.Insert(0, i);
			pnlEditLogTypeConfigInfo.Visible = true;
			pnlLogTypeConfigInfo.Visible = false;
			LogController l = new LogController();
			ArrayList arrLogTypeInfo;
			arrLogTypeInfo = l.GetLogTypeInfo();
			arrLogTypeInfo.Sort(new LogTypeSortFriendlyName());
			ddlLogTypeKey.DataTextField = "LogTypeFriendlyName";
			ddlLogTypeKey.DataValueField = "LogTypeKey";
			ddlLogTypeKey.DataSource = arrLogTypeInfo;
			ddlLogTypeKey.DataBind();
			int[] items = {
				1,
				2,
				3,
				4,
				5,
				10,
				25,
				100,
				250,
				500
			};
			ddlKeepMostRecent.Items.Clear();
			ddlKeepMostRecent.Items.Add(new ListItem(Services.Localization.Localization.GetString("All"), "*"));
			foreach (int item in items) {
				if (item == 1) {
					ddlKeepMostRecent.Items.Add(new ListItem(item.ToString() + Services.Localization.Localization.GetString("LogEntry", this.LocalResourceFile), item.ToString()));
				} else {
					ddlKeepMostRecent.Items.Add(new ListItem(item.ToString() + Services.Localization.Localization.GetString("LogEntries", this.LocalResourceFile), item.ToString()));
				}
			}
			int[] items2 = {
				1,
				2,
				3,
				4,
				5,
				10,
				25,
				100,
				250,
				500,
				1000
			};
			ddlThreshold.Items.Clear();
			foreach (int item in items2) {
				if (item == 1) {
					ddlThreshold.Items.Add(new ListItem(item.ToString() + Services.Localization.Localization.GetString("Occurence", this.LocalResourceFile), item.ToString()));
				} else {
					ddlThreshold.Items.Add(new ListItem(item.ToString() + Services.Localization.Localization.GetString("Occurences", this.LocalResourceFile), item.ToString()));
				}
			}
			ListItem j = new ListItem();
			j.Text = Services.Localization.Localization.GetString("All");
			j.Value = "*";
			ddlLogTypeKey.Items.Insert(0, j);
		}
		private void BindSummaryData()
		{
			LogController objLogController = new LogController();
			ArrayList arrLogTypeConfigInfo = objLogController.GetLogTypeConfigInfo();
			if (!Page.IsPostBack) {
				Services.Localization.Localization.LocalizeDataGrid(ref dgLogTypeConfigInfo, this.LocalResourceFile);
			}
			dgLogTypeConfigInfo.DataSource = arrLogTypeConfigInfo;
			dgLogTypeConfigInfo.DataBind();
			pnlEditLogTypeConfigInfo.Visible = false;
			pnlLogTypeConfigInfo.Visible = true;
		}
		private void DisableLoggingControls()
		{
			if (chkIsActive.Checked == true) {
				ddlLogTypeKey.Enabled = true;
				ddlLogTypePortalID.Enabled = true;
				ddlKeepMostRecent.Enabled = true;
				txtFileName.Enabled = true;
			} else {
				ddlLogTypeKey.Enabled = false;
				ddlLogTypePortalID.Enabled = false;
				ddlKeepMostRecent.Enabled = false;
				txtFileName.Enabled = false;
			}
		}
		private void DisableNotificationControls()
		{
			if (chkEmailNotificationStatus.Checked == true) {
				ddlThreshold.Enabled = true;
				ddlThresholdNotificationTime.Enabled = true;
				ddlThresholdNotificationTimeType.Enabled = true;
				txtMailFromAddress.Enabled = true;
				txtMailToAddress.Enabled = true;
			} else {
				ddlThreshold.Enabled = false;
				ddlThresholdNotificationTime.Enabled = false;
				ddlThresholdNotificationTimeType.Enabled = false;
				txtMailFromAddress.Enabled = false;
				txtMailToAddress.Enabled = false;
			}
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdCancel.Click += new EventHandler(cmdCancel_Click);
			cmdDelete.Click += new EventHandler(cmdDelete_Click);
			cmdReturn.Click += new EventHandler(cmdReturn_Click);
			cmdUpdate.Click += new EventHandler(cmdUpdate_Click);
			chkEmailNotificationStatus.CheckedChanged += new EventHandler(chkEmailNotificationStatus_CheckedChanged);
			chkIsActive.CheckedChanged += new EventHandler(chkIsActive_CheckedChanged);
			dgLogTypeConfigInfo.EditCommand += new DataGridCommandEventHandler(dgLogTypeConfigInfo_EditCommand);

			try {
				if (!Page.IsPostBack) {
					if (Request.QueryString["action"] == "add") {
						BindDetailData();
					} else {
						BindSummaryData();
					}
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdCancel_Click(System.Object sender, System.EventArgs e)
		{
			try {
				BindSummaryData();
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdDelete_Click(System.Object sender, System.EventArgs e)
		{
			LogTypeConfigInfo objLogTypeConfigInfo = new LogTypeConfigInfo();
			LogController l = new LogController();
			objLogTypeConfigInfo.ID = Convert.ToString(ViewState["LogID"]);
			try {
				l.DeleteLogTypeConfigInfo(objLogTypeConfigInfo);
				UI.Skins.Skin.AddModuleMessage(this, Services.Localization.Localization.GetString("ConfigDeleted", this.LocalResourceFile), UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
				BindSummaryData();
			} catch {
				UI.Skins.Skin.AddModuleMessage(this, Services.Localization.Localization.GetString("DeleteError", this.LocalResourceFile), UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
			}
		}
		private void cmdUpdate_Click(System.Object sender, System.EventArgs e)
		{
			LogTypeConfigInfo objLogTypeConfigInfo = new LogTypeConfigInfo();
			objLogTypeConfigInfo.LoggingIsActive = chkIsActive.Checked;
			objLogTypeConfigInfo.LogTypeKey = ddlLogTypeKey.SelectedItem.Value;
			objLogTypeConfigInfo.LogTypePortalID = ddlLogTypePortalID.SelectedItem.Value;
			objLogTypeConfigInfo.KeepMostRecent = ddlKeepMostRecent.SelectedItem.Value;
			objLogTypeConfigInfo.LogFileName = txtFileName.Text;
			objLogTypeConfigInfo.EmailNotificationIsActive = chkEmailNotificationStatus.Checked;
			objLogTypeConfigInfo.NotificationThreshold = Convert.ToInt32(ddlThreshold.SelectedItem.Value);
			objLogTypeConfigInfo.NotificationThresholdTime = Convert.ToInt32(ddlThresholdNotificationTime.SelectedItem.Value);
			objLogTypeConfigInfo.NotificationThresholdTimeType = (LogTypeConfigInfo.NotificationThresholdTimeTypes)Enum.Parse(typeof(LogTypeConfigInfo.NotificationThresholdTimeTypes), ddlThresholdNotificationTimeType.SelectedItem.Value);
			objLogTypeConfigInfo.MailFromAddress = txtMailFromAddress.Text;
			objLogTypeConfigInfo.MailToAddress = txtMailToAddress.Text;
			LogController l = new LogController();
			if (ViewState["LogID"] != null) {
				objLogTypeConfigInfo.ID = Convert.ToString(ViewState["LogID"]);
				l.UpdateLogTypeConfigInfo(objLogTypeConfigInfo);
				UI.Skins.Skin.AddModuleMessage(this, Services.Localization.Localization.GetString("ConfigUpdated", this.LocalResourceFile), UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
			} else {
				objLogTypeConfigInfo.ID = Guid.NewGuid().ToString();
				l.AddLogTypeConfigInfo(objLogTypeConfigInfo);
				UI.Skins.Skin.AddModuleMessage(this, Services.Localization.Localization.GetString("ConfigAdded", this.LocalResourceFile), UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
			}
			BindSummaryData();
		}
		private void cmdReturn_Click(System.Object sender, System.EventArgs e)
		{
			Response.Redirect(Common.Globals.NavigateURL(), true);
		}
		private void chkEmailNotificationStatus_CheckedChanged(System.Object sender, System.EventArgs e)
		{
			DisableNotificationControls();
		}
		private void chkIsActive_CheckedChanged(System.Object sender, System.EventArgs e)
		{
			DisableLoggingControls();
		}
		public void dgLogTypeConfigInfo_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			string LogID = Convert.ToString(dgLogTypeConfigInfo.DataKeys[e.Item.ItemIndex]);
			ViewState["LogID"] = LogID;
			BindDetailData();
			LogController l = new LogController();
			LogTypeConfigInfo objLogTypeConfigInfo = l.GetLogTypeConfigInfoByID(LogID);
			txtFileName.Text = objLogTypeConfigInfo.LogFileName;
			chkIsActive.Checked = objLogTypeConfigInfo.LoggingIsActive;
			chkEmailNotificationStatus.Checked = objLogTypeConfigInfo.EmailNotificationIsActive;
			if (ddlLogTypeKey.Items.FindByValue(objLogTypeConfigInfo.LogTypeKey) != null) {
				ddlLogTypeKey.ClearSelection();
				ddlLogTypeKey.Items.FindByValue(objLogTypeConfigInfo.LogTypeKey).Selected = true;
			}
			if (ddlLogTypePortalID.Items.FindByValue(objLogTypeConfigInfo.LogTypePortalID) != null) {
				ddlLogTypePortalID.ClearSelection();
				ddlLogTypePortalID.Items.FindByValue(objLogTypeConfigInfo.LogTypePortalID).Selected = true;
			}
			if (ddlKeepMostRecent.Items.FindByValue(objLogTypeConfigInfo.KeepMostRecent) != null) {
				ddlKeepMostRecent.ClearSelection();
				ddlKeepMostRecent.Items.FindByValue(objLogTypeConfigInfo.KeepMostRecent).Selected = true;
			}
			if (ddlThreshold.Items.FindByValue(objLogTypeConfigInfo.NotificationThreshold.ToString()) != null) {
				ddlThreshold.ClearSelection();
				ddlThreshold.Items.FindByValue(objLogTypeConfigInfo.NotificationThreshold.ToString()).Selected = true;
			}
			if (ddlThresholdNotificationTime.Items.FindByValue(objLogTypeConfigInfo.NotificationThresholdTime.ToString()) != null) {
				ddlThresholdNotificationTime.ClearSelection();
				ddlThresholdNotificationTime.Items.FindByValue(objLogTypeConfigInfo.NotificationThresholdTime.ToString()).Selected = true;
			}
			if (ddlThresholdNotificationTimeType.Items.FindByText(objLogTypeConfigInfo.NotificationThresholdTimeType.ToString()) != null) {
				ddlThresholdNotificationTimeType.ClearSelection();
				ddlThresholdNotificationTimeType.Items.FindByText(objLogTypeConfigInfo.NotificationThresholdTimeType.ToString()).Selected = true;
			}
			txtMailFromAddress.Text = objLogTypeConfigInfo.MailFromAddress;
			txtMailToAddress.Text = objLogTypeConfigInfo.MailToAddress;
			DisableLoggingControls();
		}
		public ModuleActionCollection ModuleActions {
			get {
				ModuleActionCollection Actions = new ModuleActionCollection();
				Actions.Add(GetNextActionID(), Services.Localization.Localization.GetString(ModuleActionType.AddContent, LocalResourceFile), ModuleActionType.AddContent, "", "", EditUrl("action", "add"), false, SecurityAccessLevel.Admin, true, false);
				return Actions;
			}
		}
	}
}
